package com.mobiftpcms.webapp.util;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.LogFactory;

import com.mobiftpcms.model.Log;
import com.mobiftpcms.model.User;
import com.mobiftpcms.service.UserExistsException;
import com.mobiftpcms.service.UserManager;

public class LogUtil {
	private static org.apache.commons.logging.Log logger = LogFactory.getLog(LogUtil.class);
	
	public static boolean log(UserManager userManager, String taskName, String taskReference, String username) {
		User user = userManager.getUserByUsername(username);
		Date time = new Date();
		
		Log log = new Log();
		log.setTaskName(taskName);
		log.setTaskReference(taskReference);
		log.setTime(time);
		
//		Set<Log> logs = user.getLogs();
//		if(logs==null) {
//			logs = new HashSet<Log>();
//		}
//		logs.add(log);
//		user.setLogs(logs);
		
		try {
			userManager.saveUser(user);
		} catch (UserExistsException e) {
			logger.warn(String.format("User %s exists", username));
			return false;
		}     
		
		return true;
	}
}
